package Variabilize;

import java.util.LinkedList;

import com.Modele;
import com.Comparaisons.Comparaison;

public class Chemin {

	private Chemin precedent = null;

	private Modele modele = null;
	private LinkedList<Comparaison> comparaisons;

	private Modele modele2 = null;

	private char[] vars1;
	private char[] vars2;

	public static final char VIDE='0';

	public Chemin copie(){
		Chemin chemin = new Chemin(modele, comparaisons.getFirst(), modele2);
		for(int i=1;i<comparaisons.size();++i){
			chemin.addComparaison(comparaisons.get(i));
		}

		if(precedent != null)
			chemin.precedent = precedent.copie();

		for(int i=0;i<modele.getArite();++i)
			chemin.vars1[i] = vars1[i];
		for(int i=0;i<modele2.getArite();++i)
			chemin.vars2[i] = vars2[i];

		return chemin;
	}

	private void initialiser_vars(){
		vars1 = new char[modele.getArite()];
		for(int i=0;i<vars1.length;++i)
			vars1[i]='0';
		vars2 = new char[modele2.getArite()];
		for(int i=0;i<vars2.length;++i)
			vars2[i]='0';
	}

	public Chemin(Modele modele, Comparaison comparaison, Modele modele2){
		this.modele = modele;
		this.comparaisons = new LinkedList<Comparaison>();
		comparaisons.addFirst(comparaison);
		this.modele2 = modele2;
		initialiser_vars();

	}

	public Chemin(Chemin precedent, Modele modele, Comparaison comparaison, Modele modele2){
		this.precedent = precedent;
		this.modele = modele;
		this.comparaisons = new LinkedList<Comparaison>();
		comparaisons.addFirst(comparaison);
		this.modele2 = modele2;
		initialiser_vars();
	}

	public Modele getModele(){
		return modele;
	}
	public Modele getModele2(){
		return modele2;
	}
	public void addComparaison(Comparaison c){
		comparaisons.addLast(c);
	}
	public Comparaison getComparaison(){
		return comparaisons.getFirst();
	}
	public Comparaison getComparaison(int i){
		return comparaisons.get(i);
	}

	public LinkedList<Chemin> getListe(){
		LinkedList<Chemin> chemins = new LinkedList<Chemin>();
		Chemin c = this;
		chemins.addFirst(c);
		while(c.precedent != null){
			c = c.precedent;
			chemins.addFirst(c);
		}
		return chemins;
	}

	public Chemin getPrecedent(){
		return precedent;
	}

	public char getPlusGrandeVariable(){
		char plus_grand = vars1[0];
		for(char c : vars1){
			if(c > plus_grand)
				plus_grand = c;
		}
		for(char c : vars2){
			if(c > plus_grand)
				plus_grand = c;
		}
		if(precedent != null){
			char c = precedent.getPlusGrandeVariable();
			if(c > plus_grand)
				plus_grand = c;
		}
		return plus_grand;
	}

	public char getLettreModele1(int i){
		if(i<vars1.length)
			return vars1[i];
		else
			return '1';
	}

	public char getLettreModele2(int i){
		if(i<vars2.length)
			return vars2[i];
		else
			return '1';
	}

	public String toString(){
		StringBuffer sb = new StringBuffer();

		if(precedent != null){
			sb.append(precedent.toString()).append(comparaisons).append(" ^ ");
			sb.append(modele2);
			sb.append("(");
			for(int i=0;i<vars2.length;++i){
				sb.append(vars2[i]);
				if(i!=vars2.length-1)
					sb.append(",");
			}
			sb.append(")");
			sb.append(" ");
		}
		else{
			sb.append(modele);
			sb.append("(");
			for(int i=0;i<vars1.length;++i){
				sb.append(vars1[i]);
				if(i!=vars1.length-1)
					sb.append(",");
			}
			sb.append(")");
			sb.append(" ").append(comparaisons).append(" ^ ");
			sb.append(modele2);
			sb.append("(");
			for(int i=0;i<vars2.length;++i){
				sb.append(vars2[i]);
				if(i!=vars2.length-1)
					sb.append(",");
			}
			sb.append(")");
			sb.append(" ");
		}

		return sb.toString();
	}

	public void setVarModele1(int n0,char c) {
		if(n0<vars1.length)
			vars1[n0]=c;
	}

	public void setVarModele2(int n1,char c) {
		if(n1<vars2.length)
			vars2[n1]=c;		
	}

}
